package blog.controller;
import blog.entity.Article;
import blog.service.DatabaseService;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="master/*")
public class ArticleController {
@Autowired DatabaseService databaseService;
	
	//list Article
		@RequestMapping(value="article/searchArticle")
		public String listArticle(ModelMap modelMap){
			List<Article> artc = databaseService.listArticle();
			modelMap.put("article",artc.toArray());
			return "/master/article/searchArticle";
		}
		
		//Find by ID 
		@RequestMapping(value="article/detailArticle", method=RequestMethod.GET)
		public String openArticle(@ModelAttribute Article artc, ModelMap modelMap){
			
			artc = databaseService.findArticleById(artc.getIda());
			modelMap.put("article", artc);
			return "/master/article/detailArticle";
		}
		
		//update article
		@RequestMapping(value="article/detailArticle", method=RequestMethod.POST)
		public String updateArticle(@ModelAttribute Article artc){
			
			databaseService.saveArticle(artc);
			return "redirect:searchArticle";
		}
		
		//insert article
		@RequestMapping(value="article/insertArticle")
		public String insertArticle(){
			return "/master/article/insertArticle";
		}
		
		@RequestMapping(value="article/insertArticle", method = RequestMethod.POST)
		public String saveArticle(@ModelAttribute Article artc){
			databaseService.saveArticle(artc);
			return "redirect:searchArticle";
		}
		
		//delete
		@RequestMapping(value="article/deleteArticle")
		public String deleteArticle(Article artc){
			databaseService.deleteArticle(artc);
			return "redirect:searchArticle";
		}
		
		@RequestMapping(value="article/upload")
		public String uploadExcel(@ModelAttribute Article artc, 
				HttpServletRequest req, HttpServletResponse res)
				throws IOException, ServletException, JXLException {
			
			ServletInputStream is = req.getInputStream();
			byte[] junk = new byte[1024];
			
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			
			Workbook workbook = Workbook.getWorkbook(is);
			
			Sheet s = workbook.getSheet(0);
			int row = s.getRows();
			int col = s.getColumns();
			Cell data = null;
			
			for(int i = 1; i < row; i++){
				for(int j = 1; j < col; j++){
					data = s.getCell(j, i);
					artc.setTitle(data.getContents());
					artc.setDesc(data.getContents());
					artc.setDate(data.getContents());
					artc.setIda((Integer) null);
					databaseService.saveArticle(artc);
				}
			}
			return "redirect:searchArticle";
		}
}
